package com.jbit.dm212.interceptor;

import cn.hutool.core.bean.BeanUtil;
import com.jbit.dm212.common.RedisUtils;
import com.jbit.dm212.common.UserUtil;
import com.jbit.dm212.entity.po.DmUser;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class UserInterceptor implements HandlerInterceptor {
    @Resource
    private RedisUtils redisUtils;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("拦截器执行了");
        String token = request.getHeader("token");
        DmUser user = redisUtils.get(token,DmUser.class);
        if (user == null){
            response.sendRedirect("/");
            return false;
        }else{
            UserUtil.setUser(user);
            return true;
        }

    }
}
